package cn.bincker.mycaptcha.utils;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RandomUtils {
    private static final Random random = new Random();
    private static final String LETTERS = "0123456789abcdefghijkmlnopqrstuvwxyzABCDEFGHIJKMLNOPQRSTUVWXYZ";

    public static String randomString(int len){
        var result = new StringBuilder();
        for (int i = 0; i < len; i++) {
            result.append(LETTERS.charAt(random.nextInt(LETTERS.length())));
        }
        return result.toString();
    }

    public static <T> List<T> select(T[] arr, int length) {
        var result = new ArrayList<T>(length);
        for (int i = 0; i < length; i++) {
            result.add(arr[random.nextInt(arr.length)]);
        }
        return result;
    }

    public static Color randomColor() {
        return new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
    }

    @SuppressWarnings("unchecked")
    public static <T> T selectOne(T ...arr) {
        if(arr == null || arr.length < 1) throw new IllegalArgumentException("empty array");
        return arr[random.nextInt(arr.length)];
    }

    public static int nextInt(int min, int max) {
        if(min == max) return min;
        if(max < min){
            int tmp = max;
            max = min;
            min = tmp;
        }
        return min + random.nextInt(max - min);
    }
}
